AS := as
LD := ld -m elf_x86_64
AS_FLAGS := -g
LD_FLAGS := -Ttext 0x0 -s --oformat binary
# -Ttext 0x0: 表示将文本段起始地址设置为0x0处
# -s: 去除所有符号
# --oformat binary: 设置输出文件格式为二进制文件

image: linux.img

linux.img : tools/build bootsect setup # kernel/system
	./tools/build bootsect setup kernel/system > $@

tools/build: tools/build.c
	gcc -o $@ $<

# kernel/system : kernel/head.S kernel/*.c mm/*.c kernel/blk_drv/*.c
# 	cd kernel; make system; cd ..

bootsect : bootsect.o
	$(LD) $(LD_FLAGS) -o $@ $<

bootsect.o: boot/bootsect.S
	$(AS) $(AS_FLAGS) -o $@ $<

setup: setup.o
	$(LD) $(LD_FLAGS) -e _start_setup -o $@ $<
# -e _start_setup: 指定程序入口地址

setup.o: boot/setup.S 
	$(AS) $(AS_FLAGS) -o $@ $<

# chr_drv/chr_drv.a: chr_drv/*.c
# 	cd chr_drv; make chr_drv.a; cd ..

.PHONY: clean

clean:
	rm -f *.o
	rm -f bootsect
	rm -f setup
	rm -f tools/build
	rm -f linux.img
# 	cd kernel; make clean; cd ..
# 	cd tools; make clean; cd ..
